<?php

namespace App\Service;

use App\Controller\Hint\usermoney;
use App\Entity\CronTabTask;
use App\Entity\Money;
use App\Entity\MoneyCashLog;
use App\Entity\MoneyFreezeLog;
use App\Entity\MoneyLog;
use App\Entity\MoneyPayOrder;
use App\Entity\MoneyType;
use App\Entity\PlatIncome;
use App\Entity\PlatIncomeLog;
use App\Entity\User;
use App\Repository\ConfigRepository;
use App\Repository\CronTabTaskRepository;
use App\Repository\MoneyConfigRepository;
use App\Repository\MoneyFreezeLogRepository;
use App\Repository\MoneyLogRepository;
use App\Repository\MoneyLogTypeRepository;
use App\Repository\MoneyRepository;
use App\Repository\MoneyStaticRepository;
use App\Repository\MoneyTypeRepository;
use App\Repository\PlatIncomeLogRepository;
use App\Repository\PlatIncomeRepository;
use App\Repository\UserRepository;
use App\Service\Wechat\WechatPay;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use function Doctrine\ORM\QueryBuilder;

class MoneyService extends DocumentRepository
{

    //资金账户设置
    const __MONEY_XIAN_JIN__ = 1;
    const __MONEY_JI_FEN__ = 2;


    //日志类型设置


    public $userRepository;
    public $moneyRepository;
    public $moneyLogRepository;
    public $moneyTypeRepository;
    public $moneyStaticRepository;
    public $moneyLogTypeRepository;
    public $configRepository;

    public function __construct(
        DocumentManager $dm
        , UserRepository $userRepository
        , MoneyRepository $moneyRepository
        , MoneyLogRepository $moneyLogRepository
        , MoneyTypeRepository $moneyTypeRepository
        , MoneyStaticRepository $moneyStaticRepository
        , MoneyLogTypeRepository $moneyLogTypeRepository
        , ConfigRepository $configRepository
    )
    {
//        parent::__construct($registry, Money::class);
        parent::__construct($dm, $dm->getUnitOfWork(), $dm->getClassMetadata(Money::class));
        $this->userRepository = $userRepository;
        $this->moneyRepository = $moneyRepository;
        $this->moneyLogRepository = $moneyLogRepository;
        $this->moneyTypeRepository = $moneyTypeRepository;
        $this->moneyStaticRepository = $moneyStaticRepository;
        $this->moneyLogTypeRepository = $moneyLogTypeRepository;
        $this->configRepository = $configRepository;
    }


    public function mod(User $user, $money, $moneyTypeId, $logTypeId, $tips = "", $param = [])
    {//通用资金添加，增加资金的时候变动余额，增加记录，改变统计信息
        $moneyType = $this->moneyTypeRepository->find($moneyTypeId);
        $logType = $this->moneyLogTypeRepository->findOneBy(array("code" => $logTypeId));
        $userMoney = $this->moneyRepository->getUserMoneyByType($user, $moneyType);
        $curMoney = $userMoney->getMoney();
        //变更余额
        $userMoney->setMoney($curMoney + $money);
        $this->dm->persist($userMoney);

        //变更数据统计
        $userStatic = $this->moneyStaticRepository->getUserMoneyStaticByType($user, $moneyType, $logType);
        $curStaticMoney = $userStatic->getMoney();
        $userStatic->setMoney($curStaticMoney + $money);
        $this->dm->persist($userStatic);

        //增加流水记录
        $MoneyLog = new MoneyLog();
        $MoneyLog->setMoney($money)
            ->setUser($user)
            ->setMoneyType($moneyType)
            ->setLogType($logType)
            ->setCreateAt(new \DateTime())
            ->setTips($tips)->setParam($param);
        $this->dm->persist($MoneyLog);
        $this->dm->flush();
//        $this->dm->commit();
    }


    /**
     * @param User $user
     * @return mixed
     * @throws \Doctrine\ODM\MongoDB\LockException
     * @throws \Doctrine\ODM\MongoDB\Mapping\MappingException
     */
    public function getUserMoney(User $user)
    {
        $money["yue"] = 0;//1余额
        $money["jifen"] = 0;//2积分

        $yueType = $this->moneyTypeRepository->find(1);
        $money["yue"] = $this->moneyRepository->getUserMoneyByType($user, $yueType)->getMoney();
        $jifenType = $this->moneyTypeRepository->find(2);
        $money["jifen"] = $this->moneyRepository->getUserMoneyByType($user, $jifenType)->getMoney();
        return $money;
    }

    public function userCash(User $user, $money)
    {
        //修改资金变动
        self::mod($user, $money * -1, self::__MONEY_XIAN_JIN__, 201, "余额提现", []);

        $card = $user->getMoneyCards()->getValues()[0] ?? null;
        //添加提现记录
        $MoneyCashLog = new MoneyCashLog();
        $MoneyCashLog->setMoney($money)
            ->setUser($user)
            ->setStatus(0)
            ->setCreateAt(new \DateTime())
            ->setMessage("")
            ->setCard($card);
        $this->dm->persist($MoneyCashLog);

        $this->dm->flush();
    }

//    public function userCashSuccess(MoneyCashLog $MoneyCashLog,$message="已付款")
//    {
//        $MoneyCashLog->setDealAt(new \DateTime())
//            ->setMessage($message)
//            ->setStatus(1)
//        ;
//        $this->dm->persist($MoneyCashLog);
//
//        $this->dm->flush();
//    }

    public function userCashFail(MoneyCashLog $MoneyCashLog, $message = "提现被拒")
    {
        $user = $MoneyCashLog->getUser();
        $money = $MoneyCashLog->getMoney();
        //修改资金变动
        self::mod($user, $money, self::__MONEY_XIAN_JIN__, 109, "提现失败返回", []);

        //修改提现记录
        $MoneyCashLog->setDealAt(new \DateTime())
            ->setMessage($message)
            ->setStatus(2);
        $this->dm->persist($MoneyCashLog);

        $this->dm->flush();
    }

    public function getUserLog(User $user, $moneyTypeId, $logTypeCode, $page = 1, $limit = 10)
    {
        $moneyType = $this->moneyTypeRepository->find($moneyTypeId);
        $logType = $this->moneyLogTypeRepository->findOneBy(array("code" => $logTypeCode));
        $condition = array(
            "user" => $user,
            "moneyType" => $moneyType
        );
        if ($logTypeCode) {
            $condition["logType"] = $logType;
        }
        $data = $this->moneyLogRepository->findBy(
            $condition,
            array("id" => "DESC")
            , $limit, ($page - 1) * $limit
        );
        return $data;
    }
}